package com.ruku.RuKucontroller;

import com.bean.WmsTBill;
import com.bean.WmsTBillDetails;
import com.bean.WmsTCustomer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruku.RuKuservice.RuKuService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RuKuController {
    @Autowired
    RuKuService ruKuService;

    @RequestMapping("rukubanli.do")//入库办理
    public String rukubanli(){
        return "ruku/rukubanli";
    }
    @RequestMapping("yhzpan.do")//验货装盘
    public String yhzpan(HttpServletRequest request){
        List<WmsTBill> WmsTBills = ruKuService.selectAllYbzpan();
        request.setAttribute("WmsTBills",WmsTBills);
        request.getSession().setAttribute("WmsTBills",WmsTBills);
        return "ruku/yhzpan";
    }

    @RequestMapping("zpmxi.do") //装盘明细                  //////////判断用户类型，，非管理员用户不能修改，返回原页面
    public String zpmxi(String choosebillId,HttpServletRequest request){
        request.setAttribute("choosebillId",choosebillId);
        request.getSession().setAttribute("choosebillId",choosebillId);
        System.out.println(choosebillId);
        List<WmsTBillDetails> wmsTBillDetails = ruKuService.selectAllBiDeTailsBybillId(choosebillId);
        List<WmsTBill> wmsTBills = (List<WmsTBill>) request.getSession().getAttribute("WmsTBills");
        for (WmsTBill evebill:wmsTBills
             ) {
            if (choosebillId.equals(evebill.getBillId())){
                request.setAttribute("choosename",evebill.getCustName());
            }
        }
        request.setAttribute("wmsTBillDetails",wmsTBillDetails);
        return "ruku/zpmxi";
    }

    @RequestMapping("yanhuoshanchu.do")//验货删除
    public String yanhuoshanchu(String choosebillDetilId,HttpServletRequest request,HttpServletResponse response) throws IOException {
        String[] as = choosebillDetilId.split(",");//根据逗号分割字符串
        for (int i = 0; i < as.length; i++) {
            System.out.println(as[i]);
            ruKuService.deleteYanHuoBydetailsId(as[i]);
        }
        String choosebillId222 = (String) request.getSession().getAttribute("choosebillId");
        List<WmsTBillDetails> wmsTBillDetails = ruKuService.selectAllBiDeTailsBybillId(choosebillId222);
        request.setAttribute("wmsTBillDetails",wmsTBillDetails);
        response.setCharacterEncoding("gbk");
        response.getWriter().print("<script>alert('删除成功！')</script>");
        return "ruku/zpmxi";
    }
    @RequestMapping("yanhuojieshu.do")//验货结束
    public String yanhuojieshu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String choosebillId333 = (String) request.getSession().getAttribute("choosebillId");
        ruKuService.updateBillStateBybillId(choosebillId333);
        List<WmsTBill> WmsTBills = ruKuService.selectAllYbzpan();
        request.setAttribute("WmsTBills",WmsTBills);
        response.setCharacterEncoding("gbk");
        response.getWriter().print("<script>alert('修改成功！')</script>");
        return "ruku/yhzpan";
    }



    @RequestMapping("rkjsuan.do") //入库结算
    public String rkjsuan(HttpServletRequest request){
        List<WmsTBill> wmsTBillsJieSuan = ruKuService.selectBillByrkjSuan();
        List<Double> aa = new ArrayList<>();
        List<Double> bb = new ArrayList<>();
        List<Integer> wmsTBillDetails11length= new ArrayList<>();
        for (WmsTBill eveBill:wmsTBillsJieSuan
             ) {
            List<WmsTBillDetails> wmsTBillDetails11 = ruKuService.selectAllBiDeTailsBybillId(eveBill.getBillId());
            wmsTBillDetails11length.add(wmsTBillDetails11.size());

            eveBill.setTuoPanGeShu(String.valueOf(wmsTBillDetails11.size()));
//            System.out.println(wmsTBillDetails11.size());
            for (int i = 0; i < wmsTBillDetails11.size(); i++) {
               double a =   (wmsTBillDetails11.get(i).getDetailsGoodsNum()) * (wmsTBillDetails11.get(i).getDetailsGoodsSpec());
//                System.out.println(a);
                aa.add(a);
//                System.out.println(aa);
            }
            eveBill.setWmsTBillDetails(wmsTBillDetails11);
        }
//        System.out.println(wmsTBillDetails11length);//每个货主有多少的商品（托盘）的集合
        //拿到两个数组：aa = [400.0, 70.0, 100.0]; wmsTBillDetails11length = [2, 1]
//        System.out.println(aa);
//        System.out.println(wmsTBillDetails11length);
        for (int i = 0; i < wmsTBillDetails11length.size(); i++) {
            double c = 0;
            for (int j = 0; j < aa.size(); j++) {

                c = c + aa.get(j);
                aa.remove(j);
//                System.out.println(c);
            }
            bb.add(c);
        }
//        System.out.println(bb);//每个货主的所有商品的重量
        Double aDouble11 = 0.0;
        WmsTBill wmsTBill11 = new WmsTBill();
        for (int i = 0; i < wmsTBillsJieSuan.size(); i++) {
            wmsTBill11 = wmsTBillsJieSuan.get(i);

            aDouble11 = bb.get(i);
            wmsTBill11.setZhongLiang(String.valueOf(aDouble11));
            wmsTBillsJieSuan.remove(i);
            wmsTBillsJieSuan.add(i,wmsTBill11);
        }

//        System.out.println(wmsTBillsJieSuan);
        request.setAttribute("wmsTBillsJieSuan",wmsTBillsJieSuan);
        request.getSession().setAttribute("wmsTBillsJieSuan",wmsTBillsJieSuan);
//        System.out.println("可以显示");
        return "ruku/rkjsuan";
    }

    @RequestMapping("rkjsuan2.do")
    @ResponseBody//ajax用
    public WmsTBill rukujisuan2(String eveId66,HttpServletRequest request){
//        System.out.println(eveId66);
        WmsTBill wmsTBill66 = new WmsTBill();
        List<WmsTBill> wmsTBillsJieSuan66 = (List<WmsTBill>) request.getSession().getAttribute("wmsTBillsJieSuan");

        for (WmsTBill eveWms:wmsTBillsJieSuan66
             ) {
            if (eveWms.getBillId().equals(eveId66)){
                wmsTBill66 = eveWms;
            }
        }
//        System.out.println(wmsTBill66.getBillFeeOther());
        request.getSession().setAttribute("wmsTBill66",wmsTBill66);
        System.out.println(wmsTBill66.getWmsTBillDetails());
        return wmsTBill66;
    }

    @RequestMapping("rkjsuan3.do")
    public String rkjsuan3(String rukudanhao,Double lizifeiyong,Double qitafeiyong,HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println(rukudanhao);
        System.out.println(lizifeiyong);
        System.out.println(qitafeiyong);
        String a= "2";
        Double billPayable1 = lizifeiyong+qitafeiyong;
        WmsTBill wmsTBillnew = new WmsTBill();
        wmsTBillnew.setBillState(a);
        wmsTBillnew.setBillId(rukudanhao);
        wmsTBillnew.setBillFeeTruckage(lizifeiyong);
        wmsTBillnew.setBillFeeOther(qitafeiyong);
        wmsTBillnew.setBillPayable(billPayable1);
        ruKuService.updateBillByrkjsuan(wmsTBillnew);
        response.sendRedirect("rkjsuan.do");

        List<WmsTBill> wmsTBillsJieSuan = (List<WmsTBill>) request.getSession().getAttribute("wmsTBillsJieSuan");
        request.setAttribute("wmsTBillsJieSuan",wmsTBillsJieSuan);

//        request.getSession().removeAttribute("wmsTBill66");
        System.out.println("结算成功");
        response.setCharacterEncoding("gbk");
        response.getWriter().print("<script>alert('结算成功！')</script>");


        return "ruku/rkjsuan";
    }


    @RequestMapping("rukuselectCusmer.do")//查询所有用户信息
    @ResponseBody//ajax用
    public List<WmsTCustomer> rukuselectCusmer(String custName){
        System.out.println(custName);
        List<WmsTCustomer> rukuselectwmsTCustomersMoHu = ruKuService.rukuselectCusmer(custName);
        return rukuselectwmsTCustomersMoHu;
    }


    @RequestMapping("banliruku.do")//入库办理
    public String banliruku(String HuZuNameAndId,String rukudan,String billDate, String billFeeStore, String billFeeTruckage, String billFeeOther,HttpServletResponse response) throws IOException {

        System.out.println(billDate);

        int i = HuZuNameAndId.indexOf("-", 0);
        String strId = HuZuNameAndId.substring(0,i);//
        String strName = HuZuNameAndId.substring(i+1,HuZuNameAndId.length());
        System.out.println(strId);
        System.out.println(strName);

        System.out.println(rukudan);//传不过来
        System.out.println(billDate);//传不过来

//        Date date1 = Date.valueOf(billDate);

//        String time = simpleDateFormat.format(date);
//        System.out.println(time);

        WmsTBill wmsTBill001 = new WmsTBill();
        wmsTBill001.setBillTransactor("001");//登录人员的id

        wmsTBill001.setBillId(rukudan);
        wmsTBill001.setCustomerId(strId);//货主的id
        wmsTBill001.setBillState("0");
        wmsTBill001.setBillDate(billDate);
        wmsTBill001.setBillEditTime(billDate);
        wmsTBill001.setBillType("0");
        Double billFeeStore1 = Double.valueOf(billFeeStore);
        Double billFeeTruckage1 = Double.valueOf(billFeeTruckage);
        Double billFeeOther1 = Double.valueOf(billFeeOther);
        System.out.println(billFeeStore1+"   "+billFeeTruckage1+"   "+billFeeOther1);
        Double billPayable = billFeeOther1+billFeeStore1+billFeeTruckage1;
        wmsTBill001.setBillFeeStore(billFeeStore1);
        wmsTBill001.setBillFeeTruckage(billFeeTruckage1);
        wmsTBill001.setBillFeeOther(billFeeOther1);
        wmsTBill001.setBillPayable(billPayable);
        wmsTBill001.setBillPaid(billPayable);
        wmsTBill001.setBillIsDel(0);

        ruKuService.ruKuBanLiSetWmstBill(wmsTBill001);
        System.out.println("成功");

        response.setCharacterEncoding("gbk");
        response.getWriter().print("<script>alert('办理入库成功！')</script>");

        return "ruku/rukubanli";
    }


    //----------------------货物入库
    @RequestMapping("hwrku.do")
    public String hwrku(HttpServletRequest request){
        List<Integer> list = new ArrayList<>();
        list.add(5);list.add(10);list.add(15);
        int pageNum = 1;
        int pageRows = 15;
        PageHelper.startPage(pageNum,pageRows);
        PageInfo<WmsTBill> pageInfo = new PageInfo<>();
        request.getSession().setAttribute("list",list);
        request.getSession().setAttribute("pageRows",pageRows);
        if(pageInfo!=null){
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "ruku/huowuruku";
    }

    @RequestMapping("hwrkuSelectData.do") //获取分页页面数据
    public String hwrkuSelectData(String dingweiId,int pageRows, String newpageNum, String billDate, String billEditTime, String huozhuName,String rukudanhao, HttpServletRequest request) throws UnsupportedEncodingException {
        System.out.println("我是newpageNum"+newpageNum);
        System.out.println(dingweiId);
        Map<String,Object> map = new HashMap<>();
        System.out.println(pageRows+"--"+newpageNum+"--"+billDate+"--"+billEditTime+"--"+huozhuName+"--"+rukudanhao);
        request.getSession().removeAttribute("billDate");
        request.getSession().removeAttribute("billEditTime");
        request.getSession().removeAttribute("huozhuName");
        request.getSession().removeAttribute("rukudanhao");


        int pageNum1 = 1;
        if(huozhuName!=null){
            if(!huozhuName.equals("")){
//                custName = new String(custName.getBytes("ISO-8859-1"),"utf-8");
                map.put("huozhuName",huozhuName);
                request.getSession().setAttribute("huozhuName",huozhuName);
            }
        }
        if(rukudanhao!=null){
            if(!rukudanhao.equals("")){
                map.put("rukudanhao",rukudanhao);
                request.getSession().setAttribute("rukudanhao",rukudanhao);
            }
        }
        if(billDate!=null){
            if(!billDate.equals("")){
                map.put("billDate",billDate);
                request.getSession().setAttribute("billDate",billDate);
            }
        }
        if(billEditTime!=null){
            if(!billEditTime.equals("")){
                map.put("billEditTime",billEditTime);
                request.getSession().setAttribute("billEditTime",billEditTime);
            }
        }
        if(newpageNum!=null){
            System.out.println(newpageNum);
            pageNum1=Integer.parseInt(newpageNum);
        }

        List<Map<String,Object>> wmsTBillDetails1 = ruKuService.selectAllByMap(billDate,billEditTime,huozhuName,rukudanhao);
        System.out.println("wmsTBillDetails1:"+wmsTBillDetails1);
        request.getSession().setAttribute("pageRows",pageRows);
        PageHelper.startPage(pageNum1,pageRows);

        String custName = huozhuName;
        if(dingweiId.equals("1")){
            List<Map<String,Object>> wmsTBillDetails = ruKuService.selectAllByMap(billDate,billEditTime,huozhuName,rukudanhao);
            PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(wmsTBillDetails);
            if(pageInfo!=null){
                System.out.println(pageInfo);
                request.getSession().setAttribute("pageInfo",pageInfo);
            }
            return "ruku/huowuruku";
        }
        else if(dingweiId.equals("2")){
            List<WmsTBill> wmsTBills = ruKuService.selectAllByRuKuDanChaXun(billDate,billEditTime,huozhuName,rukudanhao);

            System.out.println("我是Bill啊啊啊"+wmsTBills);
            PageInfo<WmsTBill> pageInfo = new PageInfo<WmsTBill>(wmsTBills);

            if(pageInfo!=null){
                System.out.println(pageInfo);
                request.getSession().setAttribute("pageInfo",pageInfo);
            }
            return "ruku/rukudanchaxun";
        }else{
            List<Map<String,Object>> wmsTBillDetails2 = ruKuService.selectAllByRuKuDan(billDate,billEditTime,huozhuName,rukudanhao);
            System.out.println("我是Bill啊啊啊"+wmsTBillDetails2);
            PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(wmsTBillDetails2);
            if(pageInfo!=null){
                System.out.println(pageInfo);
                request.getSession().setAttribute("pageInfo",pageInfo);
            }
            return "ruku/rukumxchaxun";
        }
    }


    @RequestMapping("hwrkuSelectData2.do")//货物入库点击提交修改字段billState = '3'
    public String hwrkuSelectData2(String mingxibianhao,String tuopanbianhao,String cangku,String cangwei,HttpServletResponse response) throws IOException {
        System.out.println(mingxibianhao);
        System.out.println(tuopanbianhao);//没用
        System.out.println(cangku);System.out.println(cangwei);//拿不到值
//        ruKuService.tuJiaoFromHWRku(mingxibianhao);      //点击提交修改字段billState = '3'
        response.setCharacterEncoding("gbk");
        response.getWriter().print("<script>alert('提交成功！')</script>");
        return "ruku/huowuruku";
    }

    @RequestMapping("selectDetilsByHeId.do")
    @ResponseBody//ajax用
    public WmsTBillDetails selectDetilsByHeId(String eveBillsRuKuId){
        WmsTBillDetails wmsTBillDetails = ruKuService.selectDetilsByHeId(eveBillsRuKuId);
        return wmsTBillDetails;
    }

    //----------------------入库单查询（删除修改功能）
    @RequestMapping("rkdcxun.do")
    public String rkdcxun(HttpServletRequest request){
        List<Integer> list = new ArrayList<>();
        list.add(5);list.add(10);list.add(15);
        int pageNum = 1;
        int pageRows = 15;
        PageHelper.startPage(pageNum,pageRows);
        PageInfo<WmsTBill> pageInfo = new PageInfo<>();
        request.getSession().setAttribute("list",list);
        request.getSession().setAttribute("pageRows",pageRows);
        if(pageInfo!=null){
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "ruku/rukudanchaxun";
    }

    @RequestMapping("selectBillsByHeId.do")
    @ResponseBody//ajax用
    public WmsTBill selectBillsByHeId(String eveBillsRuKuDanId){
        System.out.println("我执行了");
        WmsTBill wmsTBill = ruKuService.selectBillsByHeId(eveBillsRuKuDanId);
        return wmsTBill;
    }

    @RequestMapping("updateRuKuDanChaXun.do")
    @ResponseBody
    public Boolean updateRuKuDanChaXun(String rukudanhao1,String value1,String lizifei1,String yewushijian1,String zuihoubianjishijian1,String danjuzhuangtai1){
        System.out.println(rukudanhao1);
        System.out.println(value1);
        System.out.println(lizifei1);
        System.out.println(yewushijian1);
        System.out.println(zuihoubianjishijian1);
        System.out.println(danjuzhuangtai1);
        if(value1.equals("1")){
            //执行删除操作：修改删除字段值为1
            boolean b = ruKuService.updateWmsBills1Byrukudanhao(rukudanhao1);
            return true;
        }else{
            //执行修改操作
            boolean c = ruKuService.updateWmsBills2Byrukudanhao(rukudanhao1, lizifei1, yewushijian1, zuihoubianjishijian1, danjuzhuangtai1);
            return false;
        }


    }
    //----------------------入库明细查询
    @RequestMapping("rkmxcxun.do")
    public String rkmxcxun(HttpServletRequest request){
        List<Integer> list = new ArrayList<>();
        list.add(5);list.add(10);list.add(15);
        int pageNum = 1;
        int pageRows = 15;
        PageHelper.startPage(pageNum,pageRows);
        PageInfo<WmsTBill> pageInfo = new PageInfo<>();
        request.getSession().setAttribute("list",list);
        request.getSession().setAttribute("pageRows",pageRows);
        if(pageInfo!=null){
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "ruku/rukumxchaxun";
    }

    @RequestMapping("selectDetilsByHeDId.do")
    @ResponseBody//ajax用
    public WmsTBillDetails selectDetilsByHeDId(String eveBillsRuKuMingXiId){
        WmsTBillDetails wmsTBillDetails = ruKuService.selectDetilsByHeId(eveBillsRuKuMingXiId);
        return wmsTBillDetails;
    }
}
